--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 登录处理，php消息透传

local skynet = require "skynet"
local log = require "Log"
local md5 = require "md5"
local crypt = require "crypt"
local cjson = require "cjson"
local httpc = require "http.httpc"
local msgpack = require "msgpack_helper"
local AssertEx = AssertEx

local Login = class("Login")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function Login:ctor(message,userObj)
	self.userObj = userObj
	self.testid = 0
	self.phpHost = skynet.getenv('php_host') --php登录服务地址
	self.msg = message
	self.tbUserLogin = {}
	self.tbPassPHP = {}

	self:register()

end

--注册消息
function Login:register()	
	self.msg:registerSelf("php_req",handler(self,self.phpReq))
	self.msg:registerSelf("heart_req",handler(self,self.heart))

	self.msg:registerSelf("login_req",handler(self,self.loginReq))
	self.msg:registerSelf("Login_child_game_req",handler(self,self.onLoginChildGame))
end

--http透传
function Login:httpPass(url,content,callback)
    -- print("url_______:",url..content)
    local status, body
    --异步处理
    skynet.fork(function()
		status, body = httpc.get(url, content)
		callback(status,body)
    end)
end
---------------------------------------------------------
-- REQUEST
---------------------------------------------------------
function Login:phpReq(u_info, args)
	-- print('php_req>>>>>>>>>>>>>>>>>>',u_info, args)
	local u_info = u_info.u_info or u_info
	if not u_info.contype then
		log.error("_____u_info__error___",u_info)
		return 
	end 
	local id = tonumber(args.id)
	if id == 10000 or id == 10001 then
		self:userLogin({u_info=u_info,args=args})
	elseif id == 10002 then 
		local r = {php_res={id=id,content=""}}
		for k,v in pairs(r) do
			msgpack.send_message(u_info.fd, u_info.contype, k, v)
		end		
		local data = cjson.decode(args.content)
		local playerid = data.mid
		print("____playerid__",playerid)
		self.userObj:enterGate(u_info,playerid)	
	else
		self:passPHP({u_info=u_info,args=args})
	end

end

--登录消息
function Login:userLogin(data)
	local u_info = data.u_info
	local args = data.args	
 	local id = args.id
 	local conent =  args.content
 	local content = '/qpmain/index.php?_param=' ..conent

 	local function callback(status, body)
		if status == 200 then 	
		 	log.debug("___http_res:",body)
		 	local r = {php_res={id=id,content=body}}
		 	local isOnline = true
			for k,v in pairs(r) do
				if not msgpack.send_message(u_info.fd, u_info.contype, k, v) then
					isOnline = false
					log.error('#########send_message faild on handle cmd:'..body)
				end
			end 	
		 	--登录成功后让其进入游戏大厅
		 	if isOnline then 
		 		local content = cjson.decode(body)
		 		local res = false
		 		local errmsg = '进入游戏错误！用户不存在'		 		
		 		if content and content.code == 200 and content.result and content.result.mid then 
		 			--php登录成功
			 		local playerid = content.result.mid			 		
			 		if playerid then 
			 			res = self.userObj:enterGate(u_info,playerid)			 			
			 		end
			 		if not res then 
			 			msgpack.send_message(u_info.fd, u_info.contype,'php_res', {err_no=id,err_msg=errmsg,id=id})
			 			return
			 		end			 		
			 	end
		 	end			
	 	else
	 		print("######http login status error", status)
			if not msgpack.send_message(u_info.fd, u_info.contype,'php_res', {err_no=id,err_msg='http error '..status,id=id}) then
				-- log.error('send_message faild on handle cmd:')
			end				
	 	end
 	end
 	log.debug("Login___ "..content)
 	----透传给PHP异步回调
 	self:httpPass(self.phpHost,content,callback)
end


function Login:passPHP(data)
	local u_info = data.u_info
	local args = data.args	
 	local id = args.id
 	local conent =  args.content
 	local conent = '/qpmain/index.php?_param=' ..conent
 	local function callback(status, body)
		if status == 200 then	 	
		 	log.debug("___http_res:",body)
		 	local r = {php_res={id=id,content=body}}
			for k,v in pairs(r) do
				if not msgpack.send_message(u_info.fd, u_info.contype, k, v) then
					log.error('#########send_message faild on handle cmd:'..body)
				end
			end
	 	else
	 		print("######http status error", status)
			if not msgpack.send_message(u_info.fd, u_info.contype,'php_res', {err_no=id,err_msg='http error '..status,id=id}) then
				-- log.error('send_message faild on handle cmd:'..body)
			end				
	 	end
 	end
 	log.debug(conent)
 	----透传给PHP异步回调
 	self:httpPass(self.phpHost,conent,callback)
end

--心跳
function Login:heart()
	--print("###########heart#########")
	return {heart_res={tick=os.time()}}
end

function Login:loginReq(u_info, args)	
	local u_info = u_info.u_info or u_info
	if not u_info.contype then
		log.error("_____u_info__error___",u_info)
		return 
	end 
	local playerid = args.playerid
	local key = args.key
	--key验证完后进入游戏
	if not playerid or type(playerid) ~= 'number' then 
		return 
	end

	local res = self.userObj:enterGate(u_info,playerid)	
	--玩家ID错误
	AssertEx(res, 'login_res' , 8) 
	return {
		login_res={
			succese = true
		}
	}
end

function Login:onLoginChildGame(userinfo, data)
	print("LoginChildGame-----------data:", userinfo, data) 
	local sql = string.format("select olkey from online where uid = %d;", data.uid)
	print("sql", sql)
	local res = skynet.call("mysql_service", "lua", "execute", sql)
	print("111111111-----------res:",res) 
	local function isRobet(uid)
		 return uid < 5000000
	end
	 
	local result = {Login_child_game_res={}}
	if (res and res[1] and res[1].olkey and res[1].olkey == data.key) 
		or isRobet(data.uid) then
		if data.gameid == 9  then --中发白
			local sql1 = string.format("select sub_id from common_room where gameid = %d;", data.gameid)
			local res1 = skynet.call("mysql_service", "lua", "execute", sql1)
			local  sub_id = res1[1] and res1[1].sub_id
			print("--------------->sub_id", sql1 ,res1)
			result = {Login_child_game_res = {room_type = sub_id}}
		end
		self.userObj:enterGate(userinfo, data.uid)
	else
		--AssertEx(false, 'Login_child_game_res' , 41)	
		self.userObj:enterGate(userinfo, data.uid)	
	end

	print("------------------->result",result)
	return result
end

return Login